//
//  TodayViewController.swift
//  TestWidget
//
//  Created by Clover on 2019/3/13.
//  Copyright © 2019 clover.com. All rights reserved.
//

import UIKit
import NotificationCenter
import SnapKit

class TodayViewController: UIViewController, NCWidgetProviding {
    let topVC  = TopViewController()
    let newsVC = NewsInfoViewController()
    let bottomVC = BottomViewController()
    
//MARK: - life
    override func viewDidLoad() {
        super.viewDidLoad()
        self.preferredContentSize = CGSize(width: 0, height: 110);
        self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
        self.addBaseUI()
    }
    
//MARK: - Layout
    func addBaseUI() {
        self.addChild(topVC)
        self.view.addSubview(topVC.view)
        
        self.addChild(newsVC)
        self.view.addSubview(newsVC.view)
     
        self.addChild(bottomVC)
        self.view.addSubview(bottomVC.view)
        
        self.autoLayout()
    }
    
    func autoLayout() {
        topVC.view.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(110)
        }
        newsVC.view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(topVC.view.snp.bottom)
            make.height.equalTo(300)
        }
        bottomVC.view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(newsVC.view.snp.bottom)
            make.height.equalTo(60)
        }
    }
    
    //MARK: - NCWidgetProviding
    func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
        
        completionHandler(NCUpdateResult.newData)
    }
    
    /// 收起 && 展开
    func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
        if activeDisplayMode == .compact{
            self.preferredContentSize = CGSize(width: 0, height: 110)
        }else{
            self.preferredContentSize = CGSize(width: 0, height: 470)
        }
    }
    
}
